package pri.hillchen.std.mybatisplus.config.mybatis;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;
import org.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by hillchen on 2017/9/30 0030.
 */
@Configuration
@EnableConfigurationProperties({MyBatisProperties.class})
public class MybatisConfiguration implements EnvironmentAware {
    private ResourceLoader resourceLoader = new DefaultResourceLoader();
    private Environment environment;
    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Bean
    public GlobalConfiguration globalConfig(){
        GlobalConfiguration globalConfiguration = new GlobalConfiguration();
        globalConfiguration.setIdType(2);
        //globalConfiguration.setMetaObjectHandler(new MyMetaObjectHandler());
        return globalConfiguration;
    }

    /**
     *   mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource,MyBatisProperties myBatisProperties) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

        if (StringUtils.hasText(myBatisProperties.getMapperLocations())) {
            bean.setMapperLocations(new Resource[] {this.resourceLoader.getResource(myBatisProperties.getMapperLocations())});
        }

        if (StringUtils.hasText(myBatisProperties.getConfigLocation())) {
            bean.setConfigLocation(this.resourceLoader.getResource(myBatisProperties.getConfigLocation()));
        }
        bean.setTypeAliasesPackage(myBatisProperties.getTypeAliasesPackage());
        bean.setGlobalConfig(globalConfig());
        return bean.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage(environment.resolvePlaceholders("${"+MyBatisProperties.PRI_MYBATIS_CONFIG_PREFIX+".mapperScanner.basePackage}"));
        return mapperScannerConfigurer;
    }
}
